﻿--[[
   @brief    GameTipsMgr
   @author   seanyxie, hanmanhuang
   @version  1.0
   @date     2016-04-25
--]]

base_utility.log("loading gametipsmgr.lua");

GameTipsMgr = {
    inst_;
    ctrlid2tips_ = {};
    table_count_ = 0;

    game_id_list_ = {};
    game_id_count_ = 0;

    viewed_game_tips_inited_ = false;
    
    -- 跑马灯信息
    marquee_list_num_ = 0;
    marquee_msg_list_ = nil;

----------------------mini模式下tips处理---------------------------
-- 新建一个banner tips
NewTips = function(base_info, wrapper_id, banner)
    local tips_list, tips_count = GameTipsMgr.get_game_tips_list(base_info);
    GameTipsMgr.NewOneTips(base_info.id, wrapper_id, tips_list, tips_count, banner);
end;

-- 获取当前游戏已配置的tips信息
get_game_tips_list = function(base_info)
    tips1 = {};
    tips2 = {};
    tips3 = {};
    tipslist = {tips1 ,tips2 ,tips3};
    -- 构建gametips
    if base_info.game_tip_count > 3 then
        base_info.game_tip_count = 3;
    end;
    if base_info.game_tip_count < 0 then
        base_info.game_tip_count = 0;
    end;

    if base_info.game_tip_count > 0  then
        for i = 1,base_info.game_tip_count do 
            tipslist[i].iGame_id = base_info.game_tips[i-1].game_id;
            tipslist[i].dwMsg_id = base_info.game_tips[i-1].msg_id;
            tipslist[i].szDis_type = base_info.game_tips[i-1].dis_type;
            tipslist[i].dwPriority = base_info.game_tips[i-1].priority;
            tipslist[i].szColor = base_info.game_tips[i-1].color;
            tipslist[i].szMsg = base_info.game_tips[i-1].msg;
            tipslist[i].szUrl = base_info.game_tips[i-1].url;
            tipslist[i].szBegin_time = base_info.game_tips[i-1].begin_time;
            tipslist[i].szEnd_time = base_info.game_tips[i-1].end_time;
            base_utility.log("[GameTipsMgr]push:" .. base_info.game_tips[i-1].game_id .. "," ..
                                base_info.game_tips[i-1].msg_id .. 
                                base_info.game_tips[i-1].msg .. 
                                base_info.game_tips[i-1].begin_time .. " - " ..
                                base_info.game_tips[i-1].end_time);
        end;
    end;

    return tipslist, base_info.game_tip_count;
end;

NewOneTips = function(game_id, wrapper_id, tipslist, tipscount, banner)
    local tips = GameTips:new(game_id, wrapper_id, tipslist, tipscount, banner);
    GameTipsMgr.ctrlid2tips_[wrapper_id] = tips;
    GameTipsMgr.table_count_ = GameTipsMgr.table_count_ + 1;
    return tips;
end;

get_tips_by_ctrlid = function(tips_ctrl_id)
    local wrapper_id = ui_utility.get_ctrl_parent_id(tips_ctrl_id);
    return GameTipsMgr.ctrlid2tips_[wrapper_id];
end;

----------------------游戏中心走马灯tips处理---------------------------

get_gc_marquee_info = function(event_id, json)
    base_utility.log("[GameTipsMgr]get_gc_marquee_info, marquee_list"..json);
    if not GameTipsMgr.marquee_msg_list_ then
        GameTipsMgr.update_gc_marquee_info();
    end
    local info = base_utility.json_to_table(json);
    GameTipsMgr.notify_web_marquee_info(info);
end;


notify_web_marquee_info = function(info)
    if nil == info then
        info = {};
    end
    info.list = GameTipsMgr.get_current_marquee_info();
    if nil == info.list then
        info.list = {};
        info.list.length = 0;
    end;
    local json_str = base_utility.table_to_json(info);
    base_utility.log("[GameTipsMgr]notify_web_marquee_info, marquee_list"..json_str);
    event_center.send_event(main_web_event.SEND_GC_MARQUEE_INFO, json_str);
end;

get_current_marquee_info = function()
    if nil == GameTipsMgr.marquee_list_num_ or 
       nil == GameTipsMgr.marquee_msg_list_ then
        base_utility.log("[GameTipsMgr]get_current_marquee_info, marquee info error!");
        return nil;
    end;

    local list = {};
    list.length = GameTipsMgr.marquee_list_num_ or 0;
    local mlist = GameTipsMgr.marquee_msg_list_.astEntryList;

    if nil == mlist then
        base_utility.log("[GameTipsMgr]get_current_marquee_info, astEntryList is nil!");
        return nil;
    end

    for i = 0, list.length, 1 do
        if nil == mlist[i] or nil == mlist[i].szContent then
            base_utility.log("[GameTipsMgr]marquee index:"..i.." error!");
            return nil;
        end;
        base_utility.log("[GameTipsMgr]get_current_marquee_info, szContent"..mlist[i].szContent);
        if mlist[i] then
            list[i] = {};
            list[i].id = mlist[i].ullMsgID;
            list[i].content = mlist[i].szContent;
            list[i].url = mlist[i].szLinkUrl;
        end;
    end;
    return list;
end;

----------------------展开界面模式下的tips处理---------------------------
-- 判断游戏是否有可显示的tips, 并返回优先级最高的那条
has_tips_to_show = function(game_id)
    if viewed_game_tips_inited_ == false then
        base_utility.log("viewed_game_tips_inited_ not inited");
        return false;
    end

    if nil == game_id then
        base_utility.log("game_id is nil");
        return false;
    end
    local base_info = banner_model.get_game_base_info(game_id);
    if nil == base_info then
        return false;
    end
    local tips_list, tips_count = GameTipsMgr.get_game_tips_list(base_info);
    
    local can_show = false;
    local chosen_tips = nil;
    if tips_count == 0 then
        -- base_utility.log("has_tips_to_show, no tips");
        return can_show;
    end;

    -- 挑选要显示tips
    local valid_tips = {};
    local count = 0;

    for i = 1, tips_count do
        if GameTipsMgr.is_tips_can_show_now(tips_list[i]) then
            can_show = true;
            count = count + 1;
            valid_tips[count] = tips_list[i];
        end
    end;
    
    local init_priority = 0;
    local index = 1;
    if count >= 1 then
        for i = 1,count do
            if valid_tips[i].dwPriority > init_priority then
                init_priority = valid_tips[i].dwPriority;
                index = i;
            end;
        end;
    else
        index = 0;
    end;

    if index ~= 0 then
        chosen_tips = valid_tips[index];
    end;

    return can_show, chosen_tips;
end;

add_banner_game_id = function(game_id)
    if game_id == nil then
        return;
    end
    base_utility.log("add_banner_game_id :"..game_id);
    GameTipsMgr.game_id_count_ = GameTipsMgr.game_id_count_ + 1;
    GameTipsMgr.game_id_list_[GameTipsMgr.game_id_count_] = game_id;
end;


----------------------公共信息部分---------------------------
-- 判断当条tips是否能在当前时间显示
is_tips_can_show_now = function(tips)
    local currenttime = os.date();
    if tips ~= nil then
        local begin_list = Split(tips.szBegin_time," ");
        local begin_date_str = string.format("%s",begin_list[1]);
        local begin_time_str = string.format("%s",begin_list[2]);
        local begin_data_list = Split(begin_date_str,"-");
        local being_time_list = Split(begin_time_str,":");
        local t1={
            year = begin_data_list[1],
            month = begin_data_list[2],
            day = begin_data_list[3],
            hour = being_time_list[1],
            min = being_time_list[2],
            sec = being_time_list[3],
        };
        begindate = os.time(t1);

        -- entime 转换
        local end_list = Split(tips.szEnd_time," ");
        local end_date_str = string.format("%s",end_list[1]);
        local end_time_str = string.format("%s",end_list[2]);
        local end_data_list = Split(end_date_str,"-");
        local end_time_list = Split(end_time_str,":");
        local t2={
            year = end_data_list[1],
            month = end_data_list[2],
            day = end_data_list[3],
            hour = end_time_list[1],
            min = end_time_list[2],
            sec = end_time_list[3],
        };
        enddate = os.time(t2);

        --current string 转换
        local cur_list = Split(currenttime," ");
        local cur_date_str = string.format("%s",cur_list[1]);
        local cur_time_str = string.format("%s",cur_list[2]);
        local cur_data_list = Split(cur_date_str,"/");
        local cur_time_list = Split(cur_time_str,":");
        local t3={
            year = cur_data_list[3]+2000,
            month = cur_data_list[1],
            day = cur_data_list[2],
            hour = cur_time_list[1],
            min = cur_time_list[2],
            sec = cur_time_list[3],
        };
        curdate = os.time(t3);
    --    local begin_time = os.date(tips.szBegin_time);
    --    local end_time = os.date(tips.szEnd_time);
        if curdate > begindate and curdate < enddate then
            base_utility.log("in show time");
            return GameTipsMgr.is_tips_can_still_show_today(tips);
        else
            return false;
        end;
    else
        return false;
    end;
end;

-- 判断tips今天还能不能继续显示
-- 对于everyday类型的tips,今天点击过后，下次重启平台，不再显示，要对比todayviewed
-- 对于once类型的tips，点击一次后，就记录到了所有已经读过的tips，要对比viewed 
is_tips_can_still_show_today = function(tips)
    if tips.szDis_type == "Everyday" then
        if nil == GameTipsMgr.tips_id_in_today_filter(tips.dwMsg_id) then
            return true;
        else
            return false;
        end;
    elseif tips.szDis_type == "Once" then
        if nil == GameTipsMgr.tips_id_in_all_filter(tips.dwMsg_id) then
            return true;
        else
            return false;
        end;
    else
        return true;
    end;
end;

-- 是否今天已经看过
tips_id_in_today_filter = function(msg_id)
    t = string.find(GameTipsMgr.todayviewed, tostring(msg_id));
    return t;
end;

-- 是否已经看过
tips_id_in_all_filter = function(msg_id)
    local t = nil;
    if GameTipsMgr.viewed ~= nil then
        t = string.find(GameTipsMgr.viewed,tostring(msg_id));
    end;
    return t;
end;

-- 更新当前已看过的tips信息
update_viewed_game_tips = function(event, json_str)
    --self.viewed = string.split(viewed,".");
    --self.todayviewed = string.split(todayviewed,".");
    base_utility.log("update_viewed_game_tips :"..json_str);
    local param_table = base_utility.json_to_table(json_str);
    GameTipsMgr.viewed = param_table["viewed"];
    GameTipsMgr.todayviewed = param_table["today_viewed"];
    
    viewed_game_tips_inited_ = true;
end;

-- 开启定时器
start_tick = function()
    if GameTipsMgr.inst_ == nil then
        base_utility.log("[GameTipsMgr]add tick");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval("GameTipsMgr.tick_count", 5 * 60 * 1000);
        GameTipsMgr.inst_ = 1;
    end;
end;

-- 定时处理
tick_count = function()
    -- 刷新展开界面Banner红点
    for i = 1, GameTipsMgr.game_id_count_ do
        local banner = WGBannerList:get_banner(GameTipsMgr.game_id_list_[i]);
        if banner ~= nil then
            banner:update_game_tips_red_point();
        end
    end;
end;

detach = function()
    base_utility.log("[GameTipsMgr]detach");
    if GameTipsMgr.inst_ == 1 then
        base_utility.log("[GameTipsMgr]remove tick");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval("GameTipsMgr.tick_count");   
        GameTipsMgr.inst_ = nil;
    end;
end;

clear_all_tips = function()
    base_utility.log("clear_all_tips");
    GameTipsMgr.ctrlid2tips_ = {};
    GameTipsMgr.table_count_ = 0;
    
    GameTipsMgr.game_id_list_ = {};
    GameTipsMgr.game_id_count_ = 0;
end;
};